﻿@using Jb.Framework.Common.Forms
@using Jb.Framework.Common.Forms.Validation
@using Jb.Framework.Web
@using Jb.Framework.Web.Mvc
@model  JbTextBox

@{

    var textBoxHtmlAttr = new Dictionary<string, object>();
    var cssClasses = new List<string>();

    cssClasses.Add(Model.Size.ToDescription());
    if (cssClasses.Any())
    {
        textBoxHtmlAttr.Add("class", string.Join(",", cssClasses));
    }

    textBoxHtmlAttr.Add("placeholder", Model.PlaceHolder);

    if (Model.CurrentMode != AccessMode.Design)
    {
        if (Model.Validations.Any(x => x.Type != ElementValidationType.None))
        {
            textBoxHtmlAttr.Add("data-val", "true");
        }
        if (Model.Validations.Any(x => x.Type == ElementValidationType.Required))
        {
            textBoxHtmlAttr.Add("data-Val-Required", Model.Validations.First(x => x.Type == ElementValidationType.Required).Message);
        }

        if (Model.CurrentMode == AccessMode.ReadOnly)
        {
            textBoxHtmlAttr.Add("ReadOnly", true);
        }

    }
    var titlevalue = Model.Title;

    // if field is required add star
    if (Model.Validations.Any(t => t.Type == ElementValidationType.Required))
    {
        titlevalue += " <span class='star'>*</span>";
    }
    var labelHtmlAttr = new Dictionary<string, object> { { "class", "Custom-label" }, { "data-value", titlevalue } };
    
}

<div class="pull-left form-group" style="display: @(Model.VisibleMode != VisibilityMode.Both && Model.VisibleMode != (VisibilityMode)ViewBag.VisibleMode ? "none" : "block")">
    @for (var i = 0; i < Model.Validations.Count; i++)
    {
        //Custom model binder require full model name , i add Validations[i] at the end of string that mvc generate
        var hiddnModelBindername = "Validations[" + i + "].QVModelType"; 
        @Html.Hidden(hiddnModelBindername, Model.Validations[i].GetType())
       

        @Html.HiddenFor(x => x.Validations[i].IsChecked)
        @Html.HiddenFor(x => x.Validations[i].Type)
        @Html.HiddenFor(x => x.Validations[i].Message)

        /*
        if (Model.Validations[i] is ElementRangeValidation)
        {

            // these properties is in derived class and for correct binding we nedd this
            var validations = Model.Validations[i] as ElementRangeValidation;
            var hiddenMinModelName = "Validations[" + i + "].MinValue";
            var hiddenMaxModelName = "Validations[" + i + "].MaxValue";
        @Html.Hidden(hiddenMinModelName, validations.MinValue)
        @Html.Hidden(hiddenMaxModelName, validations.MaxValue)
        }
        else if (Model.Validations[i] is ElementMaxLengthValidation)
        {
            // these properties is in derived class and for correct binding we nedd this
            var validations = Model.Validations[i] as ElementMaxLengthValidation;
            var hiddenMinModelName = "Validations[" + i + "].Length";
        @Html.Hidden(hiddenMinModelName, validations.Length)
        }
         * */

    }

    <!--To Bind Labelii to its textbox -->
    @Html.LabelFor(x => x.Title, labelHtmlAttr)

    @Html.GenerateHiddenFields(Model)

    @Html.TextBoxFor(model => model.Value, htmlAttributes: textBoxHtmlAttr)
    @if (!string.IsNullOrEmpty(Model.HelpText))
    {
        <a class="name-info" data-container="body" rel="popover" data-placement="top" data-content="@Model.HelpText"></a>
    }
    @Html.ValidationMessageFor(model => model.Value)
</div>

<script type="text/javascript">

    //get data-value and set to html to correct show value
    $(".Custom-label").each(function () {
        // alert($(this).data("value"));
        $(this).html($(this).data("value"));
    });

</script>



